//
//  STDoorSetServiceView.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/23.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STDoorSetServiceView.h"
#import "UIButton+ZYHblock.h"
#import "STTimerDayCell.h"

@implementation STDoorSetServiceView



-(void)setDoorServiceView{
    if (!_doorMagDic) {
        _doorMagDic = [STDoorServiceSetInfoModel new];
    }
    [self loadDoorManagerListView];
    [self loadSureBtn];
}

-(void)setDoorTimerSel:(NSDictionary *)dic{
    if (!_doorMagDic.storeSettingInfo) {
        STDoorSettingInfoModel * model = [STDoorSettingInfoModel new];
        _doorMagDic.storeSettingInfo = model;
    }
    _doorMagDic.storeSettingInfo.endtime = [NSString stringWithFormat:@"%@:%@:00",dic[@"endHour"],dic[@"endMinute"]];
    _doorMagDic.storeSettingInfo.starttime = [NSString stringWithFormat:@"%@:%@:00",dic[@"fromHour"],dic[@"fromMinute"]];
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [serviceTableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
}

-(void)setDoorWorkNum:(NSString *)num{
    if (!_doorMagDic.storeSettingInfo) {
        STDoorSettingInfoModel * model = [STDoorSettingInfoModel new];
        _doorMagDic.storeSettingInfo = model;
    }
    _doorMagDic.storeSettingInfo.station = [num intValue];
    
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    [serviceTableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
}


-(void)loadDoorManagerListView{
    if(!serviceTableView){
        arrTimerDays = @[@{@"name":@"周一"},@{@"name":@"周二"},@{@"name":@"周三"},@{@"name":@"周四"},@{@"name":@"周五"},@{@"name":@"周六"},@{@"name":@"周日"}];
        
        serviceTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (5*44)*2) style:UITableViewStylePlain];
        serviceTableView.delegate = self;
        serviceTableView.dataSource = self;
        serviceTableView.scrollEnabled = NO;
//        serviceTableView.separatorInset = UIEdgeInsetsZero;
        serviceTableView.backgroundColor = [UIColor clearColor];
        serviceTableView.tableFooterView = [UIView new];
        [self addSubview:serviceTableView];
    }else{
        [serviceTableView reloadData];
    }
        
    
}

-(void)loadSureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-50, SCREEN_WIDTH, 50)];
        _sureBtn.backgroundColor = ButtonBackColor;
        [_sureBtn setTitle:@"保存" forState:UIControlStateNormal];
        WeakSelf
        __weak typeof(_doorMagDic) wealDoorMagDic = _doorMagDic;
        _sureBtn.didSelectBtn = ^(UIButton * sender) {
            if ([weakSelf.delegate respondsToSelector:@selector(doorMagSave:)]) {
                [weakSelf.delegate doorMagSave:weakSelf.doorMagDic];
            }
        };
        [self addSubview:_sureBtn];
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        return 88;
    }
    return 44;
}

//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 0;
//}
//
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView * view = [UIView new];
//    view.backgroundColor = [UIColor clearColor];
//    return view;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = @"imgDetailCell";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if(indexPath.row == 0){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.section == 0) {
            cell.textLabel.text = @"门店营业时间";
            if(_doorMagDic && _doorMagDic.storeSettingInfo.starttime.length){
                cell.detailTextLabel.text =[NSString stringWithFormat:@"%@-%@",[_doorMagDic.storeSettingInfo.starttime substringToIndex:5],[_doorMagDic.storeSettingInfo.endtime substringToIndex:5]];
            }else{
                cell.detailTextLabel.text = @"";
            }
            
        }else{
            cell.textLabel.text = @"门店工位数量";
            if(_doorMagDic){
                cell.detailTextLabel.text =[NSString stringWithFormat:@"%d",_doorMagDic.storeSettingInfo.station];
            }else{
                cell.detailTextLabel.text = @"0";
            }
        }
    }else if(indexPath.row == 1){
        cell.accessoryType = UITableViewCellAccessoryNone;
        if (indexPath.section == 0) {
            cell.textLabel.text = @"选择营业周期";
            cell.detailTextLabel.text = @"";
        }else if (indexPath.section == 1) {
            cell.textLabel.text = @"可提供服务的美发设置";
            cell.detailTextLabel.text = @"";
        }
    }else if(indexPath.row == 2){
        static NSString * advIdentifier = @"advIdentifier";
        cell = [tableView dequeueReusableCellWithIdentifier:advIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:advIdentifier];
        }
        if (indexPath.section == 0) {
            UIView *collectionView = [self loadCollectionView:1];
            [cell.contentView addSubview:collectionView];
        }else if (indexPath.section == 1) {
            UIView *collectionView = [self loadCollectionView:2];
            [cell.contentView addSubview:collectionView];
        }
    }
        
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        if(indexPath.row ==1){
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, SCREEN_WIDTH)];
        }else{
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if(indexPath.row ==1){
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, SCREEN_WIDTH)];
        }else{
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 && indexPath.row==0){
        if ([self.delegate respondsToSelector:@selector(doorMagSelTimer)]) {
            [self.delegate doorMagSelTimer];
        }
    }else if(indexPath.section == 1 && indexPath.row==0){
        if ([self.delegate respondsToSelector:@selector(doorMagSelWorkNum)]) {
            [self.delegate doorMagSelWorkNum];
        }
    }
}

-(UICollectionView *)loadCollectionView:(int)type{
    float w = (SCREEN_WIDTH-(4*30)) / 4;
    float h = 30;
    
    UICollectionViewFlowLayout * flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize=CGSizeMake(w, h);
    flow.sectionInset=UIEdgeInsetsMake(0,15,0,15);
    UICollectionView * timerCollection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 88) collectionViewLayout:flow];
    [timerCollection registerClass:[STTimerDayCell class] forCellWithReuseIdentifier:@"timerDayCell"];
    timerCollection.delegate = self;
    timerCollection.dataSource = self;
    //    timerCollection.scrollEnabled=NO;
    timerCollection.showsHorizontalScrollIndicator = NO;
    timerCollection.showsVerticalScrollIndicator = NO;
    timerCollection.backgroundColor = [UIColor whiteColor];
    if (type == 1) {
        weekColView = timerCollection;
        return weekColView;
    }else if(type == 2) {
        serviceColView = timerCollection;
        return serviceColView;
    }
    return nil;
}


#pragma mark -collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:weekColView]) {
        return arrTimerDays.count;
    }else if ([collectionView isEqual:serviceColView]) {
        return self.arrServices.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"timerDayCell";
    STTimerDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    id dic;
    int num = 0;
    if ([collectionView isEqual:weekColView]) {
         dic = arrTimerDays[indexPath.row];
        num =9100;
    }else if ([collectionView isEqual:serviceColView]) {
         dic = self.arrServices[indexPath.row];
        num =9200;
    }
    [cell setOwnCollectionCellWithData:dic];
    [cell.dayBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [cell.dayBtn setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor]] forState:UIControlStateSelected];
    [cell.dayBtn addTarget:self action:@selector(actionSelWenkDay:) forControlEvents:UIControlEventTouchUpInside];
    cell.dayBtn.tag =num+indexPath.row;
    if ([collectionView isEqual:weekColView]) {
        NSString * weekStr = [NSString stringWithFormat:@"%d",(int)(indexPath.row+1)];
        if ([_doorMagDic.storeSettingInfo.workday containsString:weekStr] ) {
            cell.dayBtn.selected = YES;
        }else{
            cell.dayBtn.selected = NO;
        }
    }else if ([collectionView isEqual:serviceColView]) {
        STDoorServiceModel * model = _arrServices[indexPath.row];
        if([model isKindOfClass:[STDoorServiceModel class]]){
            NSString * serviceId = [NSString stringWithFormat:@"%d",(int)model.objectId];
            if ([_doorMagDic.categoryIds containsString:serviceId] ) {
                cell.dayBtn.selected = YES;
            }else{
                cell.dayBtn.selected = NO;
            }
        }
    }
    return cell;
}


-(void)actionSelWenkDay:(UIButton *)sender{
    sender.selected =!sender.selected;
    int num =(int)sender.tag - 9099;
    if (num < 99) {//星期
        NSString * weeKSelStr = _doorMagDic.storeSettingInfo.workday;
        NSMutableArray * arrWeek =[NSMutableArray arrayWithArray:[weeKSelStr componentsSeparatedByString:@","]];
        NSString * weekStr = [NSString stringWithFormat:@"%d",num];
        if (!sender.selected && [arrWeek containsObject:weekStr]) {
            [arrWeek removeObject:weekStr];
        }else if (sender.selected && ![arrWeek containsObject:weekStr]) {
            [arrWeek addObject:weekStr];
        }
        NSString * newWeekStr = [arrWeek componentsJoinedByString:@","];
        _doorMagDic.storeSettingInfo.workday =newWeekStr;
    }else if (num >99) {//服务
        num-=101;
        STDoorServiceModel * model = _arrServices[num];
        NSString * objectIdStr =[NSString stringWithFormat:@"%ld",model.objectId];
        NSMutableArray * arrCategory =[NSMutableArray arrayWithArray:[_doorMagDic.categoryIds componentsSeparatedByString:@","]];
        if (!sender.selected && [arrCategory containsObject:objectIdStr]) {
            [arrCategory removeObject:objectIdStr];
        }else if (sender.selected && ![arrCategory containsObject:objectIdStr]) {
            [arrCategory addObject:objectIdStr];
        }
        NSString * newCategory = [arrCategory componentsJoinedByString:@","];
        _doorMagDic.categoryIds =newCategory;
    }
}


@end
